package crl.encrypt.ext
{
	
	import com.swfwire.decompiler.SWF10Reader;
	import com.swfwire.decompiler.SWFByteArray;
	import com.swfwire.decompiler.SWFReadResult;
	import com.swfwire.decompiler.SWFReader;
	import com.swfwire.decompiler.abc.tokens.StringToken;
	import com.swfwire.decompiler.data.swf.tags.SWFTag;
	import com.swfwire.decompiler.data.swf9.tags.DoABCTag;
	
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	public class PlayerGlobalDefine
	{
		
		[Embed(source="../../../../assets/player.def",mimeType="application/octet-stream")]
		private static var airABC:Class;
		
		public var stringList:Vector.<String>;
		public var namespa:Dictionary;
		public function PlayerGlobalDefine()
		{
			stringList=new Vector.<String>();
			
			namespa=new Dictionary();
			namespa["mx.core"]=1;
			namespa["mx.utils"]=1;
		}
		public static const instance:PlayerGlobalDefine=new PlayerGlobalDefine();
		
		
		public function write(bytes:ByteArray):void{
			var swf:SWFByteArray=new SWFByteArray(bytes);
			
			var swfReader:SWFReader = new SWF10Reader();
			
			var readResult:SWFReadResult = swfReader.read(new SWFByteArray(bytes));
			
			
				
			for each(var tag:SWFTag in readResult.swf.tags){
				
				if(tag is DoABCTag){
					addDoABCTag(tag as DoABCTag);
				}
			}
			
			var result:ByteArray=new ByteArray();
			result.writeObject(stringList);
			result.position=0;
			new FileReference().save(result,"player.def");
		}
		
		public function read():Vector.<String>{
			var abc:ByteArray=new airABC();
			var o:Object=abc.readObject();
			
			for each(var item:String in o){
				
				stringList.push(item);
			}
			
			return stringList;
		}
		
		
		private function addDoABCTag(tag:DoABCTag):void{
			for each(var string:StringToken in tag.abcFile.cpool.strings){
				addString(string.utf8);
			}
		}
		
		private function addString(value:String):int{
			var index:int=stringList.indexOf(value);
			if(index==-1){
				return stringList.push(value)-1;
			}
			return index;
		}
		
	}
}